/**
 * @author 黄波
 * 创建日期：2011Sep 13, 2011
 * TODO// N/A
 */
package com.platform.user.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.platform.constant.ResponseCode;
import com.platform.os.util.Util;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 添加用户信息
 */
public class AddUserAction extends UserSupport {

	private UserInfo user;
	private boolean success;
	private String msg;
	/**
	 * @return the user
	 */
	public UserInfo getUser() {
		return user;
	}
	/**
	 * @param user the user to set
	 */
	public void setUser(UserInfo user) {
		this.user = user;
	}
	/**
	 * @return the success
	 */
	public boolean isSuccess() {
		return success;
	}
	/**
	 * @param success the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}
	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}
	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}
	@Override
	public String execute() throws Exception {
		if(user == null) {
			msg = ResponseCode.FAILURE_ADD;
		} else {
			Date date = new Date();
			user.setRegTime(date);
			user.setLastLoginTime(date);
			user.setLastLoginIp(Util.getReq().getRemoteAddr());
			UserBo bo = UserBo.getInstance();
			
			if(bo.addUser(user)) {
				msg = ResponseCode.SUCCESS_ADD;
			} else {
				msg = "用户名已存在！";
			}
		}
		success = true;
		HashMap map = new HashMap();
		map.put("msg", msg);
		map.put("success", success);
		JSONObject json = JSONObject.fromObject(map);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();
		return null;
	}
	
}
